<?php use_javascript('/js/json_parse.js') ?>
<script type="text/javascript">
function getProduits(){
	idFournisseur = document.getElementById('remise_fournisseur_id').value;
	var xhr = getXmlHttpRequest2();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
			{
			var textCandidates = xhr.responseText;
			var textCandidatesData = json_parse(textCandidates);
			refreshSelect('remise_produit_id', textCandidatesData['produits'], null, textCandidatesData['produit']);
			
		}
	};
	xhr.open("POST","<?php print url_for('ajax/getProduitsFromFournisseur'); ?>",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("fournisseur_id="+idFournisseur);
}

function getDpt(){
	var cp = document.getElementById('client_codpos').value;
	var xhr = getXmlHttpRequest2();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
			{
			var idDpt = xhr.responseText;
			document.getElementById('client_departement_id').value = idDpt;
		}
	};
	xhr.open("POST","<?php print url_for('clients/getDptFromCP'); ?>",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("cp="+cp);		
}	

function insertRemises(){
	var id_client = document.getElementById('client_id').value;
	var checkboxes = $("#clientForm input:checkbox");
	var ar = new Array();
	var i = 0;
	for (i; i<checkboxes.length; i++) { 
		var pro = new Array(checkboxes[i].value,checkboxes[i].checked);
		ar.push(pro);
	}
	var str = array2json(ar);
	var xhr = getXmlHttpRequest2();
	xhr.open("POST","<?php print url_for('clients/insertRemises'); ?>",false);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_client=" + id_client + "&checkboxes=" + str);	
	var xhrremises = getXmlHttpRequest2();
	xhrremises.open("POST","<?php print url_for('clients/getViewItems'); ?>",false);
	xhrremises.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrremises.send("ownerClass=client&ownerId=" + id_client + "&fieldName=remises&className=remise"); 
	var htmlTable = xhrremises.responseText;
	document.getElementById('list_remises').innerHTML = htmlTable;
	$("a.fancy").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'titleShow'		: false
	});
}


</script>